package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomerException;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.mapper.EmployeeMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.pojo.Employee;
import com.itheima.reggie.pojo.Setmeal;
import com.itheima.reggie.pojo.SetmealDish;
import com.itheima.reggie.service.EmployeeService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {//implements实现,extends继承,后面都是要加s

    @Autowired
    private SetmealDishService setmealDishService;


    @Override
    @Transactional
    public void saveWithDish(SetmealDto setmealDto){

        //1.保存套餐的基本信息，操作setmeal，执行insert操作
        this.save(setmealDto);


        //2.保存套餐和菜品的关联信息，操作setmeal_dish,执行insert操作
        List<SetmealDish> setmealDishList = setmealDto.getSetmealDishes();

        //2.1遍历设置每一个SetmealDish的setmealId值,,//补全套餐菜品关系
        setmealDishList.forEach(setmealDish -> setmealDish.setSetmealId(setmealDto.getId()));
        setmealDishService.saveBatch(setmealDishList);




       /* //1.保存套餐的基本信息，操作setmeal，执行insert操作
        this.save(setmealDto);

        List<SetmealDish> setmealDishList = setmealDto.getSetmealDishes();
        //补全套餐菜品关系
        setmealDishList.forEach(setmealDish -> setmealDish.setSetmealId(setmealDto.getId()));

        //2.保存套餐和菜品的关联信息，操作setmeal_dish,执行insert操作
        setmealDishService.saveBatch(setmealDishList);*/

    }


    //删除套餐，同时需要删除套餐和菜品的关联数据
    @Override
    @Transactional
    public void removeWithDish(Long[] ids) {
        //select count(*) from setmeal where id in (1,2,3) and status = 1
        //0.判断(查询)套餐状态是否在售卖，确定是否可用删除
        LambdaQueryWrapper<Setmeal> queryWrapper1 = new LambdaQueryWrapper<>();

        queryWrapper1.in(Setmeal::getId, Arrays.asList(ids));
        queryWrapper1.eq(Setmeal::getStatus, 1);
        int count = this.count(queryWrapper1);
        if (count > 0) {
            //如果不能删除，抛出一个业务异常
            throw new CustomerException("套餐正在售卖，不能删除");
        }

        //1.如果可以删除，先删除套餐表中的数据---setmeal
        this.removeByIds(Arrays.asList(ids));

        //delete from setmeal_dish where setmeal_id in (1,2,3)
        //2.删除关系表中的数据----setmeal_dish
        LambdaQueryWrapper<SetmealDish> queryWrapper2 = new LambdaQueryWrapper<>();
        queryWrapper2.in(SetmealDish::getSetmealId, Arrays.asList(ids));
        setmealDishService.remove(queryWrapper2);


       /* //上课老师写法:  0.判断(查询)套餐状态是否在售卖     select count(*) from setmeal where id in (1,2,3) and status = 1
        QueryWrapper<Setmeal> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.in(ids.size()>0,"id",ids);
        queryWrapper1.eq("status",1);
        int count = this.count(queryWrapper1);
        if (count > 0) {
            //如果不能删除，抛出一个业务异常
            throw new CustomerException("套餐正在售卖，不能删除");
        }

        //1删除套餐   delete from setmeal where id in ids
        this.removeById(ids);

        //2.删除套餐菜品数据(后台控制台下面sql语句没打印出来sql后面的ids是因为全局异常处理器没有返回值)   delete from setmeal_dish where setmeal_id in ids
        QueryWrapper<SetmealDish> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.in("setmeal_id",ids);
        setmealDishService.remove(queryWrapper2);*/
    }
    }



